package com.example.administrator.x2picturetransliteration.Http;

import com.alibaba.fastjson.JSON;
import com.example.administrator.x2picturetransliteration.Bean.HttpDataBean;
import com.example.administrator.x2picturetransliteration.Constants;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observable;

/**
 * 请求接口实现方法
 * 2018/10/9.
 */

public class DataManager {

    private RequestBody getPostJsonData(Object mPostData){
        return RequestBody.create(MediaType.parse("application/json"), JSON.toJSONString(mPostData));
    }

    /**
     * 转换为 form-data-----------------------------------------------------
     */

    /** map */
    public static Map<String, RequestBody> RequestBodyString(Map<String, String> requestDataMap) {
        Map<String, RequestBody> requestBodyMap = new HashMap<>();
        for (String key : requestDataMap.keySet()) {



            if (requestDataMap.get(key)!=null && !"".equals(requestDataMap.get(key))){

                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),String.valueOf(requestDataMap.get(key)));
                requestBodyMap.put(key, requestBody);
            }
        }

        System.out.print("");

        return requestBodyMap;
    }

    /** String */
    public static RequestBody RequestBodyString(String requestData){

        if (requestData==null)return null;

        return RequestBody.create(MediaType.parse("multipart/form-data"),requestData);
    }

    /** List */
    private List<MultipartBody.Part> getPostFormData(List<File> FileData) {

        List<MultipartBody.Part> mmp = new ArrayList<>();

        for (File files:FileData){

            if (files!=null){

                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),files);
                MultipartBody.Part filePart = MultipartBody.Part.createFormData("files", files.getName(), requestBody);
                mmp.add(filePart);
            }

        }

        return mmp;
    }

    /**
     * 转换为 form-data-----------------------------------------------------end
     */

    /**
     * 通用get请求
     * */
    public Observable<HttpDataBean> getHttpData(String url) {
        return new HttpData().getService().getHttpData(url);
    }

    /**
     * 通用post请求
     */
    public Observable<HttpDataBean> postHttpData(String url, Map<String,String> mPostData) {

        return new HttpData().getService().postHttpData(url,
                RequestBodyString(Constants.getConstants().getToken()),
                RequestBodyString(Constants.getConstants().getChannelCode()),
                RequestBodyString(mPostData));
    }

    /**
     * 上传文件
     * */
    public Observable<HttpDataBean> postHttpFile(String url, List<File> mPostData) {

        return new HttpData().getService().postHttpFile(url,
                RequestBodyString(Constants.getConstants().getToken()),
                RequestBodyString(Constants.getConstants().getChannelCode()),
                getPostFormData(mPostData));
    }

    public Observable<HttpDataBean> postHttpFile(String url, File mPostData) {

        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),mPostData);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", mPostData.getName(), requestBody);

        return new HttpData().getService().postHttpFile(url,
                RequestBodyString(Constants.getConstants().getToken()),
                RequestBodyString(Constants.getConstants().getChannelCode()),
                filePart);
    }

}
